from django.conf import settings
from rest_framework.decorators import api_view
from asgiref.sync import sync_to_async
from resource.appman.models import AppMan

from apischedule.decorators import path_validator
from apischedule.client import ApiClient
from apischedule.models import RouteSchema
from apigw.core.response import ResponseApi404

@api_view(['GET','POST'])
@path_validator
def api_bouncer(request,app,route):
    client = ApiClient(app,route)
    method = request.method
    params = request.query_params
    data = request.data
    headers = request.headers
    from_sys = request.headers.get("from_sys",settings.DEFAULT_SYS_NAME)
    return client.request(method,params,data,headers,from_sys)


# async def a_api_bouncer(request,  app_id, apigw_url, *args, **kwargs):
#     # check param
#     if not app_id:
#         return ResponseApi404(app_id)
#     if not apigw_url:
#         return ResponseApi404(app_id + '/' + apigw_url)
#     app = AppMan.objects.get_app(app_id)
#     route = RouteSchema.objects.get_url(apigw_url,app_id)
#     if not app:
#         return ResponseApi404(app_id)
#     if not route:
#         return ResponseApi404(app_id + '/' + apigw_url)
    
#     client = ApiClient(app,route)
#     method = request.method
#     params = request.query_params
#     data = request.data
#     headers = request.headers
#     from_sys = request.headers.get("from_sys",settings.DEFAULT_SYS_NAME)
#     return client.request(method,params,data,headers,from_sys)
